package com.wnnight.wys.mapper.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.wnnight.wys.entity.PageBean;
import com.wnnight.wys.entity.config.PropertyStaff;
import com.wnnight.wys.vo.config.PropertyStaffVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @ClassName: PropertyStaffMapper
 * @Description: //物业人员mapper
 * @author: LZJ
 * @date: 2022/8/28-10:48
 * @version: v1.0
 */
public interface PropertyStaffMapper extends BaseMapper<PropertyStaff> {
    @Insert("insert into wys_property_staff(name,phone,update_time,password,account,img,role_id)values(#{name},#{phone},#{updateTime},#{password},#{account},#{img},#{role.id})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void add(PropertyStaff propertyStaff);

    @UpdateProvider(type = sqlProvider.class, method = "update")
    void update(PropertyStaff propertyStaff);

    @Update("update wys_property_staff set password=#{password} where id=#{id}")
    void updatePsd(int id,String password);

    //收费配置mapper动态sql
    class sqlProvider {
        //修改
        public static String update(PropertyStaff propertyStaff) {
            String sql = new SQL() {
                {
                    UPDATE("wys_property_staff");
                    if (propertyStaff.getAccount() != null) {
                        SET("account=#{account}");
                    }
                    if (propertyStaff.getUpdateTime() != null) {
                        SET("update_time=#{updateTime}");
                    }
                    if (propertyStaff.getPassword() != null) {
                        SET("password=#{password}");
                    }
                    if (propertyStaff.getName() != null) {
                        SET("name=#{name}");
                    }
                    if (propertyStaff.getPhone() != null) {
                        SET("phone=#{phone}");
                    }
                    if (propertyStaff.getImg() != null) {
                        SET("img=#{img}");
                    }
                    if (propertyStaff.getRole() != null) {
                        SET("role_id=#{role.id}");
                    }
                    WHERE("id=#{id}");
                }
            }.toString();
            return sql;
        }
    }

    @Select("select wps.*,wr.id from wys_property_staff as wps left join  wys_role as wr on wps.role_id=wr.id"+"${ew.customSqlSegment}")
    IPage<PropertyStaff> getAll(IPage<PropertyStaff> page, @Param(Constants.WRAPPER) QueryWrapper<PropertyStaff> queryWrapper);
    //根据物业人员的id查对应的角色拥有的权限
    @Select("select * from wys_property_staff where id=#{id}")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "role_id", property = "role", many = @Many(select = "com.wnnight.wys.mapper.role.RoleMapper.getById"))
    })
    PropertyStaff getMenuById(int id);

    //通过账号密码查
    @Select("select u.*,r.name from wys_property_staff u,wys_role r where u.role_id=r.id and account=#{account} and password=#{password}")
    @ResultMap("ResultMap")
    PropertyStaff getByAccountAndPassword(String  account,String  password);

    //通过账号查
    @Select("select u.*,r.name from wys_property_staff u,wys_role r where u.role_id=r.id and account=#{account} ")
    @ResultMap("ResultMap")
    PropertyStaff getByAccount(String  account);
    //根据物业人员id 查信息，带出角色名
    @Select("select u.*,r.name from wys_property_staff u,wys_role r where u.role_id=r.id and u.id=#{id}")
    @Results(id = "ResultMap", value =
            {
                    @Result(column = "id", property = "id", id = true),
                    @Result(column = "name", property = "name"),
                    @Result(column = "phone", property = "phone"),
                    @Result(column = "area", property = "area"),
                    @Result(column = "update_time", property = "updateTime"),

                    @Result(column = "account", property = "account"),
                    @Result(column = "password", property = "password"),
                    @Result(column = "img", property = "img"),
                    @Result(column = "role_id", property = "role",many = @Many(select = "com.wnnight.wys.mapper.role.RoleMapper.getById")),
            }
    )
    PropertyStaff getById(int id);

    @Select("SELECT  wps.id , wps.name , wps.phone  FROM ${prefix}repairs_place wrp,${prefix}property_area wpa,${prefix}property_staff wps WHERE wrp.id = wpa.place_id AND wpa.staff_id = wps.id AND wrp.id= #{placeId}")
    PropertyStaff getStaffByPlaceId(int placeId);

    @Select("select img from wys_property_staff")
    List<PropertyStaff> getImg();
}
